// pages/pay/pay.js
const app = getApp()
const pathUrl = app.globalData.pathUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that =this ;
    var openId =wx.getStorageSync("openId")
    var telphone = wx.getStorageSync("telphone")
    var orderNo = wx.getStorageSync("orderNo")
    var loginInfo = wx.getStorageSync("loginInfo")
    var loginInfo = JSON.parse(loginInfo)
    console.log(loginInfo)
    var name = loginInfo.records[0].query13
    that.setData({
      openId:openId,
      telphone: telphone,
      orderNo: orderNo,
      name:name
    })
    this.getmoney();
  },
  // 去支付
  goPay: function() {
    wx.request({
      url: pathUrl +'/tenPayController/app/tenpay/prepaybypre',
      data:{
        openid: this.data.openId,
        orderNo: this.data.orderNo,
        appCode:'fxjpay',
      },
      method:'GET',
      success:function(e){
        console.log(e)
        console.log(e.data.code)
        console.log(e.data.noncestr)
        wx.requestPayment(
          {
            'timeStamp': e.data.timestamp,
            'nonceStr': e.data.noncestr,
            'package': e.data.package,
            'signType': 'MD5',
            'paySign': e.data.sign,
            'success': function (res) {
              console.log(res)
              wx.navigateTo({
                url: '/pages/paysuc/paysuc',
              })
             },
            'fail': function (res) { 
              console.log(res)
              wx.showModal({
                title: '提示',
                content: '支付失败',
                showCancel:false
              })
            },
            'complete': function (res) {
              console.log(res)

             }
          }
        )
      }
    })
    // wx.navigateTo({
    //   url: '../paysuc/paysuc'
    // })
  },
  getmoney: function (e) {
    var that = this;
    wx.request({
      url: pathUrl + '/gzgl/fxyGzgl06/list',
      method: 'GET',
      data: {
        query07: that.data.orderNo,
      },
      success: function (res) {
        var list = res.data.result.records[0];
        if(list ==null || list ==''){
          wx.showModal({
            title: '提示',
            content: '暂时未查询到您的付款订单',
            showCancel:false,
            success:function(res){
              if(res.confirm){
                wx.reLaunch({
                  url: '/pages/index/index',
                })
              }
            }
          })
          return false
        }
        var sfzf = list.query11;
        console.log(res.data.result.records[0].query12);

        that.setData({
          money:list.query12
        })
        if(sfzf=="付款成功"){
          wx.showModal({
            title: '提示',
            content: '该订单已付款成功',
            showCancel:false,
            // success:function(){
            //   wx.switchTab({
            //     url: '/pages/index/index',
            //   })
            // }
            success: function () {
              wx.navigateTo({
                url: '/pages/index/index',
              })
            }
          })
        
        }
      },
      fail: function (res) { 

      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})